<?php
class GalleriesController extends AppController {

	var $name = 'Galleries';
	var $components = array('Uploader.Uploader');
	function beforeFilter() {
        parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/gallery/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '5M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;

    }
    function index() {
		$this->Gallery->recursive = 0;
			$this->paginate = array(
				'conditions'=>array('status'=>2),
			'order'=> 'Gallery.id ASC'
		);
		$this->set('galleries', $this->paginate());
	}
	function chitiet($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$conditions = array('Gallery.status'=>2);
		$item = $this->Gallery->read(null, $id);
		$this->set('item',$item);
		$otheritem = $this->Gallery->find('all',array('conditions'=>array('Gallery.id <>'=>$id,'Gallery.status'=>2),'limit'=>10,'order'=>'Gallery.id DESC'));
		$this->set(compact('otheritem'));
	}
	function admin_index() {
		$this->Gallery->recursive = 0;
			$this->paginate = array(
			'order'=> 'Gallery.id DESC'
		);
		$this->set('galleries', $this->paginate());
	}
	function admin_add() {
		if(!empty($this->data)){
			$upload = $this->Uploader->uploadAll(array('url'));
			$this->data['Gallery']['url'] =  $upload['url']['name'];
			$this->Gallery->create();
			if ($this->Gallery->save($this->data)){
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('controller' =>'galleries','action' => 'index'));
			} else {
				$this->Session->setFlash(__('Có lỗi xảy ra. Hãy thử lại!', true));
			}
		}
	}
	function admin_edit($id = null) {
		$files = $this->Gallery->find('list',array(
			'conditions'=>array('Gallery.id'=>$id),
			'fields'=>'url')
		);
		//debug($files);
		$file ='';
		$file = "files/uploads/galley/" .$files[$id];
		//debug($file);
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['Gallery']['url'] = $files[$id];
            if ($data = $this->Uploader->upload('url')) {
				$tmpname  = $data['name'];
				if(is_file($file)){
					unlink($file);
				}
				$this->data['Gallery']['url'] = $tmpname;
            }
			if ($this->Gallery->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Gallery->read(null, $id);
		}
	}

	function admin_delfile($id = null){
		$this->render(false);
			$files = $this->Gallery->find('list',array(
				'conditions'=>array('Gallery.id'=>$id),
				'fields'=>'url')
			);
			$file ='';
			$file = "files" . DS . "uploads" . DS. "gallery" . DS .$files[$id];
			if(is_file($file)){
				unlink($file);
			}
			$this->Gallery->id = $id;
			$this->Gallery->saveField('url','');
			$this->redirect(array('action' => 'edit/'.$id));
	}
	function _delfile($id = null){

			$files = $this->Gallery->find('list',array(
				'conditions'=>array('Gallery.id'=>$id),
				'fields'=>'url')
			);
			$file ='';
			$file = "files" . DS . "uploads" . DS. "gallery" . DS .$files[$id];
			if(is_file($file)){
				unlink($file);
			}
			

	}
	function admin_delete($id = NULL) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại ảnh này !', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->_delfile($id);
		if ($this->Gallery->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'index'));
		}
		else{
		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
		$this->redirect(array('action' => 'index'));
		}
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
    		$this->redirect(array('action' => 'index',$str));
        }
         foreach($arrid as $item){
			$this->_delimg($item);
    		$this->Img->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index',$str));
    }

}
?>